<?php

use wechat\models\WeConfig;
use yii\web\View;

/**
 * Created by PhpStorm.
 * User: shutao
 * Date: 2018/9/13
 * Time: 16:22
 * @var View $this
 * @var WeConfig $model
 */

$this->title = '添加/编辑微信配置';

?>


<form class="layui-form">

    <div class="layui-form-item">
        <label for="type" class="layui-form-label">
            <span class="x-red">*</span><?php echo $model->getAttributeLabel('type') ?>
        </label>
        <div class="layui-input-inline">
            <select name="type" id="type" required="required" lay-verify="required">
                <option>请选择</option>
                <?php foreach (WeConfig::typeText() as $key => $text) {
                    ?>
                    <option <?php if ($key == $model->type) {
                        echo 'selected';
                    } ?>
                            value="<?php echo $key ?>"><?php echo $text ?></option>
                    <?php
                } ?>
            </select>
        </div>
    </div>

    <div class="layui-form-item">
        <label for="title" class="layui-form-label">
            <span class="x-red">*</span><?php echo $model->getAttributeLabel('title') ?>
        </label>
        <div class="layui-input-inline">
            <input type="text" name="title" required=""
                   lay-verify="required|title"
                   autocomplete="off" class="layui-input"
                   value="<?php echo $model->title ?>">
        </div>
        <div class="layui-form-mid layui-word-aux">请输入当前配置的名称（中文）</div>
    </div>

    <div class="layui-form-item">
        <label for="appid" class="layui-form-label">
            <span class="x-red">*</span><?php echo $model->getAttributeLabel('appid') ?>
        </label>
        <div class="layui-input-inline">
            <input type="text" name="appid" required=""
                   lay-verify="required"
                   autocomplete="off" class="layui-input"
                   value="<?php echo $model->appid ?>">
        </div>
    </div>

    <div class="layui-form-item">
        <label for="appsecret" class="layui-form-label">
            <span class="x-red">*</span><?php echo $model->getAttributeLabel('appsecret') ?>
        </label>
        <div class="layui-input-block">
            <input type="text" name="appsecret" required=""
                   autocomplete="off" class="layui-input"
                   value="<?php echo $model->appsecret ?>">
        </div>

    </div>

    <div class="layui-form-item">
        <label for="token" class="layui-form-label">
            <?php echo $model->getAttributeLabel('token') ?>
        </label>
        <div class="layui-input-block">
            <input type="text" name="token" id="token"
                   style="width: 250px;display: inline"
                   autocomplete="off" class="layui-input"
                   value="<?php echo $model->token ?>">
            <a class="layui-btn layui-btn-normal" onclick="genString(32,'token')">随机生成</a>
        </div>
        <div class="layui-form-mid layui-word-aux">（公众号）必须为英文或数字，长度为3-32字符。</div>
    </div>

    <div class="layui-form-item">
        <label for="token" class="layui-form-label">
            <?php echo $model->getAttributeLabel('encodingaeskey') ?>
        </label>
        <div class="layui-input-block">
            <input type="text" id="encodingaeskey" name="encodingaeskey"
                   style="width: 350px;display: inline;"
                   autocomplete="off" class="layui-input"
                   value="<?php echo $model->encodingaeskey ?>">
            <a class="layui-btn layui-btn-normal" onclick="genString(43,'encodingaeskey')">随机生成</a>
        </div>
        <div class="layui-form-mid layui-word-aux">（公众号）消息加密密钥由43位字符组成，可随机修改，字符范围为A-Z，a-z，0-9;</div>
    </div>

    <div class="layui-form-item">
        <label for="debug" class="layui-form-label"><?php echo $model->getAttributeLabel('debug') ?>
        </label>
        <div class="layui-input-inline">
            <select name="debug" id="debug">
                <option>请选择</option>
                <?php foreach (WeConfig::debugText() as $key => $text) {
                    ?>
                    <option <?php if ($key == $model->status) {
                        echo 'selected';
                    } ?>
                            value="<?php echo $key ?>"><?php echo $text ?></option>
                    <?php
                } ?>
            </select>
        </div>
    </div>

    <div class="layui-form-item">
        <label for="is_default" class="layui-form-label"><?php echo $model->getAttributeLabel('is_default') ?>
        </label>
        <div class="layui-input-inline">
            <select name="is_default" id="is_default">
                <option>请选择</option>
                <?php foreach (WeConfig::isDefaultText() as $key => $text) {
                    ?>
                    <option <?php if ($key == $model->is_default) {
                        echo 'selected';
                    } ?>
                            value="<?php echo $key ?>"><?php echo $text ?></option>
                    <?php
                } ?>
            </select>
        </div>
    </div>

    <div class="layui-form-item">
        <label for="domain" class="layui-form-label">
            <?php echo $model->getAttributeLabel('domain') ?>
        </label>
        <div class="layui-input-block">
            <input type="text" name="domain"
                   autocomplete="off" class="layui-input"
                   value="<?php echo $model->domain ?>">
        </div>
    </div>

    <div class="layui-form-item">
        <label for="debug" class="layui-form-label"><?php echo $model->getAttributeLabel('status') ?>
        </label>
        <div class="layui-input-inline">
            <select name="status" id="status">
                <option>请选择</option>
                <?php foreach (WeConfig::statusText() as $key => $text) {
                    ?>
                    <option <?php if ($key == $model->status) {
                        echo 'selected';
                    } ?> value="<?php echo $key ?>"><?php echo $text ?></option>
                    <?php
                } ?>
            </select>
        </div>
    </div>

    <div class="layui-form-item">
        <label for="qr_code" class="layui-form-label"><?php echo $model->getAttributeLabel('qr_code') ?>
        </label>
        <div class="layui-input-inline">
            <button type="button" class="layui-btn layui-btn-normal" id="qr_code">
                <i class="iconfont">&#59049;</i> 上传二维码
            </button>
        </div>
    </div>

    <div class="layui-form-item">
        <label for="L_repass" class="layui-form-label"></label>
        <input name="_csrf" type="hidden" id="_csrf" value="<?= \Yii::$app->request->csrfToken ?>">
        <?php if ($model->id) { ?>
            <input type="hidden" name="gid" value="<?php echo $model->id ?>">
        <?php } ?>
        <button class="layui-btn" lay-filter="add" lay-submit=""><?php echo $model->id ? '保存' : '增加' ?></button>
    </div>
</form>
<script>
    function genString(length, id) {
        var s = '';
        while (s.length < length) {
            s += Math.random().toString(36).substr(2);
        }
        s = s.substr(0, length);
        document.getElementById(id).value = s;
        // $('#' + id).val(s);
    }
    layui.use(['form', 'upload', 'jquery', 'layer'], function () {
        var form = layui.form, upload = layui.upload, $ = layui.$, layer = layui.layer;



        //自定义验证规则
        form.verify({
            title: function (value) {
                if (value.length < 2) {
                    return '配置的名称不能小于2个字';
                }
            },
            token: function (value) {
                if (value.length < 3 || value.length > 32) {
                    return 'token 必须为英文或数字，长度为 3-32 字符'
                }
            },
            encodingaeskey: function (value) {
                if (value.length != 43) {
                    return '请输入 43 位的 EncodingAESKey';
                }
            }
        });

        //执行实例
        var uploadInst = upload.render({
            elem: '#qr_code', //绑定元素
            url: '?r=wechat/config/edit&action=qrcode',
            accept: 'images',
            data: {'_csrf': document.getElementById('_csrf').value},
            done: function (res) { // 上传后回调, res 服务器响应信息, index 当前文件索引, upload 重新上传方法
                if (!res.success) {
                    // 1 正确, 2 错误(X), 3 询问，4 锁定, 5 不开心, 6 开心(笑脸) 7 感叹号
                    layer.alert(res.message, {icon: 2}, function (index) {
                        layer.close(index);
                    });
                } else {

                }
                //上传完毕回调
                console.log('done', res);
            }
        });

        //监听提交
        form.on('submit(add)', function (data) {
            if (isNaN(parseInt(data.field['type']))) {
                layui.layer.alert('请选择类型', {icon: 2}, function (index) {
                    layer.close(index);
                });
                return;
            }
            $.ajax({
                type: "POST",
                url: '#',
                data: data.field,
                success: function (data, textStatus, xhr) {
                    // console.log('success', textStatus, xhr);return;
                    layer.msg(data, {icon: 6, time: 1000}, function () {
                        // 获得frame索引
                        var index = parent.layer.getFrameIndex(window.name);
                        //关闭当前frame
                        parent.layer.close(index);
                        parent.location.reload();
                    });
                },
                error: function (xhr, textStatus, errorThrown) {
                    // 1 正确, 2 错误(X), 3 询问，4 锁定, 5 不开心, 5 开心(笑脸)
                    layer.alert(textStatus + ':' + xhr.responseText, {icon: 2}, function (index) {
                        layer.close(index);
                    });
                },
                dataType: 'text'
            });
            return false;
        });
    });
</script>